const app = getApp()
const WXAPI = require("../../../../api/newApi")
const WXAPIOLD = require("../../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ls:[{},{},{}],
    id: '',
    info: {},
    shareImg:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight:app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    this.data.id = options.id
    this.getInfo()
  },
  onShow(){
   
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
    //分享
  // onShareAppMessage: function () {
  //   return {
  //     title: '送你蜜链盟资产，点击领取',
  //     path: `/userPageA/pages/scan/receiving_assets/receiving_assets?id=` + this.data.id,
  //     imageUrl: this.data.shareImg
  //   }
  // },
  getInfo(){ 
    let result = false
    var data = {
      id: this.data.id
      // id: '1381538602922524674'
    }
    WXAPI.sendGive(data, (res) => {
      if (res.code == 200) {
        this.data.info = res.result
        this.setData({
          info: res.result
        })
        setTimeout(() => {
          this.canvasdraw()
        }, 100);
      }
    })
  },
  canvasdraw: function (canvas) {
    console.log(1111111)
    var that = this;
    
    const ctx = wx.createCanvasContext('canvas')
    ctx.save()
    var path = "/images/canvas_bj.png";
    ctx.drawImage(path, 0, 0, 310, 247);
    ctx.restore()
    ctx.save()
    var str = '送你'
    // ctx.font = 'normal bold 20px sans-serif';
    ctx.setTextAlign('center'); // 文字居中
    ctx.setFillStyle("#FFF0D3");
    ctx.setFontSize(40)
    ctx.fillText(str, 310/2,65)
    // ctx.draw()
    
    var str2 = '￥' + this.data.info.giveTotal
    ctx.setFontSize(40)
    ctx.setFillStyle("#FFF0D3");
    ctx.setTextAlign('center');
    ctx.fillText(str2, 310/2,125)
    ctx.restore()
    ctx.save()
    var path2 = "/images/canvas_djlq.png";
    ctx.drawImage(path2, 46, 150, 220, 54);
    ctx.restore()
    ctx.draw()
    setTimeout(()=>{
      this.daochu()
    },500)
    
  },
  daochu(){
    console.log(1111)
    var _this = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 310,
      height: 247,
      destWidth: 620,
      destHeight: 494,
      fileType: 'jpg', //图片的质量，目前仅对 jpg 有效。取值范围为 (0, 1]，不在范围内时当作 1.0 处理。
      quality: 1,
      canvasId: 'canvas',
      success(res) {
        console.log(222)
        console.log(res.tempFilePath)
        _this.data.shareImg = res.tempFilePath
        _this.setData({
          img: res.tempFilePath
        })
      }
    })
  },
  //唤醒扫一扫功能
  getScancode: function() {
    var that = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        let parameter = res.result.split("code=")[1];
        console.log(parameter)
        let result = JSON.parse(parameter)
        console.log(result)
        console.log("扫一扫参数",result.param)
        console.log("扫一扫类型",result.type)
        if(result.type==1){
          wx.navigateTo({
            url: '/userPageA/pages/scan/information/information?param=' + result.param
          })
        }else if(result.type==2){
          wx.navigateTo({
            url: '/userPageA/pages/scan/receiving_assets/receiving_assets?param=' + result.param
          })
        }else if(result.type==4){
          this.setData({
            qrCodeUrl: result.param,
          })
          this.getParCollectionQrCodeUrl()
        }
        else{
          console.log('else')
        }
      },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  //解析二维码confirmInfo
  getParCollectionQrCodeUrl(){
   
    var data = {
      qrCodeUrl:this.data.qrCodeUrl,
      // qrCodeUrl:'https://bee.xiaodaiyan.com/pay/uQRxY%2F6Y4pbfVN4%2BbISi5WEG2fcys%2Beg9h2%2B0dfXuU8%3D',
    }
    WXAPIOLD.parCollectionQrCodeUrl(data, (res) =>{
      const data = res.result
      console.log("解析二维码",res)
      if(res.code === 200){
        var merchantId = data.merchantId
        wx.navigateTo({
          url: '/pages/pay/pay?QRmerchantId='+merchantId,
        })
      }else{
        wx.showToast({
          title: '无效收款码',
          icon: 'none'
        })
      }
    })
  },
  //点击保存图片
 save () {
  let that = this
  //若二维码未加载完毕，加个动画提高用户体验
  wx.showToast({
   icon: 'loading',
   title: '正在保存图片',
   duration: 1000
  })
  //判断用户是否授权"保存到相册"
  wx.getSetting({
   success (res) {
    //没有权限，发起授权
    if (!res.authSetting['scope.writePhotosAlbum']) {
     wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success () {//用户允许授权，保存图片到相册
       that.savePhoto();
      },
      fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
       wx.openSetting({
        success () {
         wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
           that.savePhoto();
          }
         })
        }
       })
      }
     })
    } else {//用户已授权，保存到相册
     that.savePhoto()
    }
   }
  })
 },
//保存图片到相册，提示保存成功
 savePhoto() {
    let that = this
    let img = that.data.info.sendQrCode
    // console.log(that.data.info.sendQrCode)
    // console.log(this.data.Imgurl)
    var save = wx.getFileSystemManager();
    var number = Math.random();
    save.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
      data: img.slice(22),
      encoding: 'base64',
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: function (err) {
            console.log(err)
          }
        })
        console.log(res)
      }, fail: err => {
        console.log(err)
      }
    })
  },
  back(){
    wx.reLaunch({
      // url: '/pages/assets/assets',
      url:'/pages/userMarket/userMarket'
    })
  },
  jumpRecord(){ //转赠记录
    wx.navigateTo({
      url: '/userPageA/pages/wallet/gift_record/gift_record',
    })
  },
})